import express from 'express'
export const router = express.Router()
import books from '../../store/books.js'
import { createConnectionPool } from '../../mysql/index.js'

const connection = await createConnectionPool()

//一. sim 手机号 post
router.post('/api/addbooks', async (req, res) => {
  const { get_sim_number } = req.body
  console.log(`HEELO`)
  // 不能为空
  if (!get_sim_number) {
    res.status(401).send({ code: 401, msg: 'get_sim_number 参数必须传' })
    return
  }

  // 只能传 get_sim_number 这个参数
  const keys = Object.keys(req.body)
  if (keys.length !== 1 || !keys.includes('get_sim_number')) {
    res.status(401).send({ code: 401, msg: '只能传递 get_sim_number 参数' })
    return
  }

  // 正则
  if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(get_sim_number)) {
    res.status(401).send({ code: 401, msg: '请输入正确手机号' })
    return
  }

  // 判断是否存在重复get_sim_number
  const booksData = await books.getData()
  const isDuplicate = booksData.some(
    book => book.get_sim_number === get_sim_number
  )
  if (isDuplicate) {
    res.status(401).send({ code: 'error', msg: '不能添加重复值' })
    return
  }

  const str = await books.addData(req.body)
  console.log(str)

  // 添加成功后的返回
  if (str == '添加成功') {
    res.status(200).send({ code: 200, msg: '提交成功' })
  } else {
    res.status(401).send({ code: 401, msg: '提交失败' })
  }
})

//二 delete 邀请码
router.delete('/api/deletebooks', async (req, res) => {
  console.log(`hello baby`)
  let str = await books.delData(req.query.id)
  if (str == '删除成功') {
    res.status(200).send({ code: 200, msg: '删除成功' })
  } else {
    res.status(400).send({ code: 400, msg: '删除失败' })
  }
})

//二. get 邀请码
router.get('/api/get_invitation_code', async (req, res) => {
  const arr = await books.getInvitation_code()
  console.log(arr)

  if (arr instanceof Array) {
    res.status(200).send({ code: 200, msg: '获取数据成功', data: arr })
  } else {
    res.status(400).send({ code: 400, msg: `获取数据失败` })
  }
})

//二 post 邀请码
router.post('/api/add_invitation_code', async (req, res) => {
  if (!req.body.code) {
    res.status(401).send({ code: 401, msg: 'code参数必传' })
    return
  }
  if (req.body.code == '' || req.body.code == {}) {
    res.status(401).send({ code: 401, msg: '参数值不能为空' })
    return
  }

  const str = await books.addInvitation_code(req.body)
  console.log(str)
  // 添加成功后的返回
  if (str == '添加成功') {
    res.status(200).send({ code: 1, msg: '提交成功' })
  } else {
    res.status(400).send({ code: 0, msg: '提交失败' })
  }
})

//二. 判断邀请码是否存在
router.post('/api/judgment_invitation_code', async (req, res) => {
  if (!req.body.code) {
    res.status(401).send({ code: 401, msg: 'code参数必传' })
    return
  }
  if (req.body.code == '' || req.body.code == {}) {
    res.status(401).send({ code: 401, msg: '参数值不能为空' })
    return
  }
  const get_code = await books.getInvitation_code()

  const codes = get_code.map(item => item.code) // 获取json文件内所有的code值

  if (codes.includes(req.body.code)) {
    // 如果用户输入的code值存在于json文件内的code数组中
    const str = await books.addInvitation_code(req.body)
    console.log(str)
    // 添加成功后的返回
    if (str == '添加成功') {
      res.status(200).send({ code: 1, msg: '提交成功' })
    } else {
      res.status(400).send({ code: 0, msg: '提交失败' })
    }
  } else {
    // 如果用户输入的code值不存在于json文件内的code数组中
    res.status(400).send({ code: 0, msg: '提交失败，code值不存在' })
  }
})

//二 delete 邀请码
router.delete('/api/delete_invitation_code', async (req, res) => {
  let str = await books.deleteInvitation_code(req.query.id)
  if (str == '删除成功') {
    res.status(200).send({ code: 200, msg: '删除成功' })
  } else {
    res.status(400).send({ code: 400, msg: '删除失败' })
  }
  // res.send({ code: 200, msg: '删除成功' })
})

//三. 查询接口
router.get('/api/get_report_card', async (req, res) => {
  // 所有数据
  let arr = await books.get_reportCard()
  // 用户传来的 examination_number 参数
  const examinationNumber = req.query.examination_number
  if (!examinationNumber) {
    res.status(400).json({ code: 400, message: 'examinationNumber参数必须传' })
  }
  if (examinationNumber == '') {
    res.status(400).json({ code: 400, message: '参数值不能为空' })
  }

  // if (examinationNumber !== Number) {
  //   res.status(400).json({ code: 400, message: '请输入数字类型值' })
  // }
  const foundItem = arr.find(
    item => item.examination_number === examinationNumber
  )
  if (foundItem) {
    res.status(200).send({ code: 200, message: foundItem })
  } else {
    res.status(404).json({ code: 404, message: '查询失败数据不存在' })
  }
})

//四.获取个人信息
router.post('/api/user_info', async (req, res) => {
  const sql = "select * from user where username = ? and password = ?"
  const params = [req.body.userName, req.body.password]
  const result = await connection.execute(sql, params)
  if (result.length === 0) {
    res.status(400).json({ code: 400, message: '用户名或密码错误' })
  }else{
    res.status(200).json({ code: 1, data: result[0], message:'信息获取成功'})
  }
})

export default router
